from scipy import linalg,array,dot,mat,transpose
from numpy import take
import sys
import random
from context_functions import *
from lattice_functions import *
from os import system

if len(sys.argv)<2: 
	exit()
dbfile = sys.argv[1]
matrix,intent,extent = readContext(dbfile)

minsup = "3%"
universe = len(extent)
each = universe/30
samples = 10

tmatrix = transpose(array(matrix))
files = []

for i in range(0,universe,each):
	for sam in range(samples):
		auxmat = []
		auxext = []
		selind = range(len(extent))
		for j in range(i):
			k = int(random.random()*len(selind))
			selind.pop(k)
		auxext = take(extent,selind)

		for j in range(len(matrix)):
			auxmat.append(take(matrix[j],selind))
		#print array(auxmat)
		#auxmat.pop(k)
		#extent.pop(k)
		#print "Writing " + dbfile[dbfile.rfind("/")+1:].replace(".rcf","-"+str(i)+".rcf")
		perc = 100-float((100*i)/universe)
		fp = writeContext(dbfile[dbfile.rfind("/")+1:].replace(".rcf","-"+str(perc)+"_"+str(sam)+".rcf"),auxmat,intent,auxext,path="reduced/")
		files.append(fp)
		print fp

for f in files:
	system("./coron.sh " + f + " " + minsup + " " + f[f.rfind("/"):].replace(".rcf",""))

